package com.rn.simple.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.DockerCmdExecFactory;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.command.EventsResultCallback;
import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;

/**
 * Description：https://github.com/docker-java/docker-java/wiki
 *
 * @author rannuo
 * @date 2020/2/18
 */
public class DockerClientSimple {

    public static void main(String[] args) throws InterruptedException, IOException {
//        DockerClient dockerClient = DockerClientBuilder.getInstance("tcp://192.168.22.128:2375").build();
//        Info info = dockerClient.infoCmd().exec();
//        System.out.println(info);


        //进行安全认证
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerTlsVerify(true)
                .withDockerCertPath("D:\\JavaDev\\IDEA_J\\simple\\data\\certs").withDockerHost("tcp://192.168.22.128:2375")
                .withDockerConfig("D:\\JavaDev\\IDEA_J\\simple\\data\\daemon.json").withApiVersion("1.40").build();
//                .withRegistryUrl("https://index.docker.io/v1/")
//                .withRegistryUsername("dockeruser").withRegistryPassword("ilovedocker")
//                .withRegistryEmail("dockeruser@github.com").build();
//        DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
//                .withReadTimeout(1000)
//                .withConnectTimeout(1000)
//                .withMaxTotalConnections(100)
//                .withMaxPerRouteConnections(10);
//        //进行连接
//        DockerClient dockerClient = DockerClientBuilder.getInstance(config).withDockerCmdExecFactory(dockerCmdExecFactory).build();
        //进行连接
        DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();

        // 获取Docker的环境信息
//        Info info = dockerClient.infoCmd().exec();
//        System.out.println(info);

        // 搜索镜像
        List<SearchItem> dockerSearch = dockerClient.searchImagesCmd("nginx").exec();
        System.out.println("Search returned" + dockerSearch.toString());

        // pull镜像
        DockerUtils.pullImage(dockerClient, "nginx:latest");

        // 创建容器
        CreateContainerResponse container = createContainers(dockerClient, "nginx-test", "nginx");
        //启动容器
        DockerUtils.startContainer(dockerClient, container.getId());
        EventsResultCallback callback = new EventsResultCallback() {
            @Override
            public void onNext(Event event) {
                System.out.println("Event: " + event);
                super.onNext(event);
            }
        };
        dockerClient.eventsCmd().exec(callback).awaitCompletion().close();
    }

    /**
     * 创建容器
     *
     * @param client
     * @return
     */
    public static CreateContainerResponse createContainers(DockerClient client, String containerName, String imageName) {
        // 映射端口8088—>80
        ExposedPort tcp80 = ExposedPort.tcp(80);
        Ports portBindings = new Ports();
        portBindings.bind(tcp80, Ports.Binding.bindPort(8088));
        return client.createContainerCmd(imageName)
                .withName(containerName)
                .withHostConfig(HostConfig.newHostConfig().withPortBindings(portBindings))
                .withExposedPorts(tcp80).exec();
    }

}
